package com.mida.bms.report.controller;


import com.mida.bms.report.service.ImageService;
import com.mida.bms.report.vo.ImageVo;
import com.mida.tool.common.result.ActionResult;
import lombok.extern.slf4j.Slf4j;
import org.dcm4che3.tool.dcm2jpg.Dcm2Jpg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author ：bzy
 * @description：图片上传
 */
@RestController
@RequestMapping("/imageController")
@CrossOrigin
@Slf4j
public class ImageController {

    @Autowired
    private ImageService imageService;

    /**
     * 采集到的图片上传
     *
     * @param file 图片文件
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/uploadImg", produces = {"application/json;charset=UTF-8"}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public String uploadImg(@RequestPart("file") MultipartFile file, @RequestParam("postId") String postId,
                            @RequestParam("personSoid") String personSoid, @RequestParam("customerSoid") String customerSoid,
                            @RequestParam("registrationSoid") String registrationSoid, @RequestParam("loginUserId") Long loginUserId) throws IOException {

        return imageService.uploadImg(file, postId, personSoid, customerSoid, registrationSoid, loginUserId);
    }

    /**
     * 获取图片列表
     *
     * @return
     * @throws IOException
     */
    @GetMapping(value = "/queryImageList", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public List<ImageVo> queryImageList(String postId) throws IOException {
        return imageService.queryImageList(postId);
    }

    /**
     * 删除图片
     *
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/deleteImage", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ActionResult deleteImage(String postId, String imageName) {
        ActionResult result = new ActionResult();
        try {
            return imageService.deleteImage(postId, imageName);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.BAD_REQUEST.value());
            result.setMessage("删除图片失败");
            return result;
        }
    }

    /**
     * 转换dicom
     *
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/transitionDicom", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ActionResult transitionDicom() throws IOException {
        Dcm2Jpg dcm2Jpg = new Dcm2Jpg();
        File file0 = new File("E:\\1.dcm");
        File file1 = new File("E:\\x.jpg");
        dcm2Jpg.convert(file0, file1);
        return new ActionResult();

    }

    /**
     * 服务器图片上传
     *
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/decompression",produces = {"application/json;charset=UTF-8"},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public ActionResult decompression(@RequestPart("file")MultipartFile file, @RequestParam("postId")String postId,
                                @RequestParam("personSoid")String personSoid, @RequestParam("customerSoid")String customerSoid,
                                @RequestParam("registrationSoid")String registrationSoid,
                                @RequestParam("fileType")String fileType)throws IOException {
        return imageService.decompression(file, postId, personSoid, customerSoid, registrationSoid, fileType);
    }


    /**
     * 服务器图片上传(多文件)
     *
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/decompressionList",produces = {"application/json;charset=UTF-8"},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public ActionResult decompressionList(@RequestPart("file")MultipartFile[] file, @RequestParam("postId")String postId,
                                      @RequestParam("personSoid")String personSoid, @RequestParam("customerSoid")String customerSoid,
                                      @RequestParam("registrationSoid")String registrationSoid, @RequestParam("fileType")String fileType)throws IOException {
        return imageService.decompressionList(file, postId, personSoid, customerSoid, registrationSoid, fileType);


    }




    /**
     * 图片上传后维护数据库
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/maintainData")
    @ResponseBody
    public ActionResult maintainData(@RequestParam("postId")String postId, @RequestParam("personSoid")String personSoid,
                                     @RequestParam("customerSoid")String customerSoid,
                                     @RequestParam("registrationSoid")String registrationSoid,
                                     @RequestParam("imagePath")String imagePath){
        return imageService.maintainData(postId, personSoid, customerSoid, registrationSoid, imagePath);
    }


}
